package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;

import cn.itcast.utils.commons.UtilFuns;
import cn.itcast.vo.ExportVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.FileInputStream;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/cargo/packing")
public class packingController extends BaseController {

    @Reference
    private PackingService packingService;

    @Reference
    private ExportService exportService;

    @RequestMapping(value = "/list", name = "管理")//*
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {
        PackingExample packingExample = new PackingExample();
        packingExample.setOrderByClause("create_time desc");
        PackingExample.Criteria criteria = packingExample.createCriteria();
        criteria.andStateBetween(0l,1l);
        criteria.andCompanyIdEqualTo(companyId);
        PageInfo page = packingService.findAll(packingExample,pageNum, pageSize);
        request.setAttribute("page", page);
        return "cargo/packing/packing-list";
    }

    @RequestMapping(value = "/edit", name = "生成装箱单")//*
    public String edit(Packing packing) {
        packing.setCompanyId(companyId);
        //设置企业名称
        packing.setCompanyName(companyName);
        if (UtilFuns.isEmpty(packing.getPackingListId())) {
            packingService.save(packing);
        } else {
            packingService.update(packing);
        }
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "/toView",name = "查询装箱单详情")
    public String toView(String id){
        Packing packing = packingService.findById(id);
        request.setAttribute("packing",packing);
        return "cargo/packing/packing-view";
    }

    @RequestMapping(value = "toUpdate",name = "到修改页面")
    public String toUpdate(String id){
        Packing packing = packingService.findById(id);
        request.setAttribute("packing",packing);
        return "cargo/packing/packing-update";
    }

    @RequestMapping(value = "/delete",name = "删除装箱单")
    public String delete(String id){
        packingService.delete(id);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "/submit",name = "提交")
    public String submit(String id){
        Packing packing = packingService.findById(id);
        packing.setState(1l);
        packingService.update(packing);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "/cancel",name = "取消")
    public String cancel(String id){
        Packing packing = packingService.findById(id);
        packing.setState(0l);
        packingService.update(packing);
        return "redirect:/cargo/packing/list.do";
    }

    /*@RequestMapping(value = "/packingPdf",name = "委托单下的装箱下载")
    public String packingPdf(String id){
        Packing packing = packingService.findById(id);
        Map<String, Object> map = BeanMapUtils.beanToMap(packing);
        return null;
    }*/

}
